package com.xh.bussiness.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.permission.dao.SchoolMapper;
import com.xh.bussiness.permission.model.School;
import com.xh.bussiness.system.bean.request.SchoolRequest;
import com.xh.bussiness.system.service.ISchoolService;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.core.util.StringUtils;
import org.springframework.stereotype.Service;
import com.xh.core.bean.XhPage;

import java.util.List;

/**
 * <p>
 * 校区 服务实现类
 * </p>
 *
 * @author Pitt
 * @since 2020-11-24
 */
@Service
public class SchoolServiceImpl extends BaseServiceImpl<SchoolMapper, School> implements ISchoolService {
    @Override
    public XhPage querySchoolPage(XhPage page, SchoolRequest bean) {
        QueryWrapper qw=new QueryWrapper<School>();
        //拼装条件
        qw=prepareParams(bean, qw);
        qw.orderByDesc("modify_time");
        return (XhPage) this.dao.selectPage(page,qw);
    }

    @Override
    public List querySchool(SchoolRequest bean) {
        QueryWrapper qw=new QueryWrapper<School>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return this.dao.selectList(qw);
    }

    @Override
    public int saveOrUpdate(School school) {
        School exist = super.getById(school.getId());
        if(exist == null){
            super.insert(school);
        }else{
            super.update(school);
        }
        return 1;
    }

    @Override
    public School querySchoolByThirdId(String thirdId) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("third_id", thirdId);
        return this.selectFirst(qw);
    }


    /**
     * 准备条件
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(SchoolRequest bean, QueryWrapper qw) {
        if(bean.getIds()!=null&&bean.getIds().length>0){
            qw.in("id",bean.getIds());
        }
        if (!StringUtils.isEmpty(bean.getProvinceId())) {
            qw.eq("province_id", bean.getProvinceId());
        }
        if (!StringUtils.isEmpty(bean.getCityId())) {
            qw.eq("city_id", bean.getCityId());
        }
        if (!StringUtils.isEmpty(bean.getSchoolName())) {
            qw.like("school_name", bean.getSchoolName());
        }
        return qw;
    }
}
